package com.yx.doodleview.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.yx.baselibrary.base.BaseDialog;
import com.yx.baselibrary.eventbus.EventCenter;
import com.yx.doodleview.R;
import com.yx.doodleview.bean.ColorBean;
import com.yx.doodleview.ui.adapter.SelectAdapter;
import com.yx.doodleview.ui.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yx on 2017/8/18.
 */

public class ChooseDialog extends BaseDialog {
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private List<ColorBean> list;
    private int lastPostion = 1;

    private OnItemViewClickListener onItemClickListener;
    private SelectAdapter selectAdapter;

    public void setOnItemClickListener(OnItemViewClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public static ChooseDialog newInstance(List<ColorBean> list) {
        ChooseDialog chooseDialog = new ChooseDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("list", (ArrayList<ColorBean>) list);
        chooseDialog.setArguments(bundle);
        return chooseDialog;
    }


    @Override
    public void before(Bundle savedInstanceState) {
        list = getArguments().getParcelableArrayList("list");
    }

    @Override
    public void initViewAndEvent() {
        recycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        selectAdapter = new SelectAdapter(list);
        recycleview.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(list.get(position));
                }
                if (lastPostion != -1) {
                    list.get(lastPostion).setCheck(false);
                }
                list.get(position).setCheck(true);
                lastPostion = position;
                selectAdapter.notifyDataSetChanged();
                dismiss();
            }
        });
        recycleview.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL, R.drawable.divider));
        recycleview.setAdapter(selectAdapter);
    }


    @Override
    public int getContentViewLayoutID() {
        return R.layout.ui_dialog;
    }

    @Override
    public void onEventComming(EventCenter eventCenter) {

    }


    @Override
    public void initPresenter() {

    }

    @Override
    public View getLoadingTargetView() {
        return null;
    }


    @Override
    public void setLocation(Dialog dialog) {
        lp.gravity = Gravity.CENTER; //设置dialog位置
        lp.width = (int) (display.getWidth() * 0.85); // 设置dialog宽度
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;// 设置dialog高度
        window.setAttributes(lp);
    }


    public interface OnItemViewClickListener {
        void onItemClick(ColorBean colorBean);
    }

}
